package com.tboss.template.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tboss.template.system.dto.RoleDto;
import com.tboss.template.system.service.RoleService;

@Controller
@RequestMapping(value = "/template/system/role")
public class RoleController {
	
	@Autowired RoleService roleService;
	
	@RequestMapping(value = {"/_list{reqPage}", "/_list/{reqPage}", "/_listConts/{reqPage}"}, 
			method = {RequestMethod.POST, RequestMethod.GET})//
	public String roleList(@PathVariable("reqPage") Integer reqPage, @ModelAttribute RoleDto dto,
			Model model, HttpServletRequest req){

		String rtnUrl = req.getServletPath().substring(1);		
		if(reqPage != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"));
		
		this.roleService.setModelRoleList(reqPage, model, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regForm"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String roleRegForm(Model model, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		
		this.roleService.roleRegForm(model);
		
		return rtnUrl+"Ajax";
	}

	@RequestMapping(value = {"/_regFormSave"}, method = {RequestMethod.POST})//
	public Model roleRegFormSave(@ModelAttribute RoleDto dto, Model model, HttpServletRequest req){
		this.roleService.addRoleInfo(dto);

		return model;
	}
	
	@RequestMapping(value = {"/_regFormRevise/{roleId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String roleRegFormRevise(Model model, @PathVariable("roleId") String roleId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(roleId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("Revise/"+roleId));
		
		this.roleService.roleRegForm(model, roleId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regFormReviseSave"}, method = {RequestMethod.POST})//
	public Model roleRegFormReviseSave(@ModelAttribute RoleDto dto, Model model, HttpServletRequest req){
		this.roleService.updateRoleInfo(dto);

		return model;
	}
	
	@RequestMapping(value = {"/_detail/{roleId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String roleInfoDetail(Model model, @PathVariable("roleId") String roleId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(roleId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+roleId));
		
		this.roleService.roleRegForm(model, roleId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_groups/{roleId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String fnInfoGroups(Model model, @PathVariable("roleId") String roleId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(roleId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+roleId));
		
		this.roleService.roleGroups(model, roleId);
		
		return rtnUrl+"Ajax";
	}

}
